/*
 * @features: 功能
 * @description: 说明
 * @Date: 2021-09-13 15:04:52
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-10-12 23:29:58
 * @LastEditors: judu233
 */

import { JsonAsset } from "cc";
import { resLoader } from "../tool/other/ResLoader";
import { GameConfig } from "./GameConfig";
import { QueryConfig } from "./QueryConfig";
import * as buildTimeConstants from 'cc/env';
import c3d from "../tool/cc";
import { HotUpdate } from "../Asset/hotupdate/Hotupdate";

/** 游戏配置静态访问类 */
export class ConfigData {
    /** 构建时环境常量 */
    public btc!: string;

    /** 配置数据，版本号、支持语种等数据 */
    public game!: GameConfig;

    /** 处理浏览器地址栏参数，包括服务器ip、端口等数据 */
    public query!: QueryConfig;

    public init() {
        let config_name = "config/config";
        return new Promise<void>(resolve => {
            resLoader.load(config_name, JsonAsset, () => {
                var config = resLoader.get(config_name);
                this.btc = this.BuildTimeConstants();
                this.query = new QueryConfig();
                this.game = new GameConfig(config);

                resolve();
            })
        });
    }

    private BuildTimeConstants() {
        const keys = (Object.keys(buildTimeConstants) as (keyof typeof buildTimeConstants)[]).sort();
        const keyNameMaxLen = keys.reduce((len, key) => Math.max(len, key.length), 0);
        let enviroment = `${keys.map((key) => {
            const value = buildTimeConstants[key];
            const valueRep = typeof value === 'boolean' ? (value ? 'true' : 'false') : value;
            return `\n${key.padStart(keyNameMaxLen, ' ')} : ${valueRep}`;
        })}`;

        c3d.Out.logConfig(enviroment, "游戏运行环境");
        return enviroment;
    }
}


/*
 * @features: 功能
 * @description: 说明
 * @Date: 2021-09-12 19:44:52
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-10-10 18:49:06
 * @LastEditors: judu233
 */
/**全局配置 */


export namespace Config {
    /**是否显示调试按钮 */
    export const isShowDebugButton = true;

    /**公共Prefabs预置路径 */
    export const CommonPrefabs = {
        tips: "common/prefabs/Tips",
        uiLoading: "common/prefabs/UILoading",
        loading: "common/prefabs/Loading",
        alert: "common/prefabs/Alert",
    }

    /**公共音效路径 */
    export const audioPath = {
        dialog: "common/audio/dlg_open",
        button: "common/audio/btn_click",
    }

    /**是否跳过热更新检测 */
    export const isSkipCheckUpdate = false;

    /**测试热更新服务器地址 */
    export const TEST_HOT_UPDATE_URL_ROOT = "";//"http://192.168.0.104:9945/hotupdate";

    /**Loading动画显示超时回调默认超时时间 */
    export const LOADING_TIME_OUT = 30;

    /**Loading提示中切换显示内容的时间间隔 */
    export const LOADING_CONTENT_CHANGE_INTERVAL = 3;

    /**加载界面超时时间,如果在LOAD_VIEW_TIME_OUT秒未加载出，提示玩家加载界面超时 */
    export const LOAD_VIEW_TIME_OUT = 20;

    /**UILoading显示默认时间，即在打开界面时，如果界面在LOAD_VIEW_DELAY之内未显示，就会弹出一的加载界面的进度 
     * 在打开界面时，也可直接指定delay的值
     * @example  
     * Manager.uiManager.open({ type : LoginLayer, zIndex: ViewZOrder.zero, delay : 0.2});
     */
    export const LOAD_VIEW_DELAY = 0.1;

    /**大厅bundle名 */
    export const BUNDLE_HALL = "hall";

    /**重连的超时时间 */
    export const RECONNECT_TIME_OUT = 30;

    /**进入后台最大时间（单位秒）大于这个时间时就会进入重连*/
    export const MAX_INBACKGROUND_TIME = 60;
    /**进入后台最小时间（单位秒）大于这个时间时就会进入重连*/
    export const MIN_INBACKGROUND_TIME = 5;

    /**网络重连弹出框tag */
    export const RECONNECT_ALERT_TAG = 100;

    export const ENTRY_CONFIG : {[key:string] : HotUpdate.BundleConfig} ={};
}

/**
 * 界面层级定义
 */

export namespace ViewZOrder {
    /**最底层 */
    export const zero = 0;
    /**小喇叭显示层 */
    export const Horn = 10;
    /**ui层 */
    export const UI = 100;
    /**提示 */
    export const Tips = 300;
    /**提示弹出框 */
    export const Alert = 299;
    /**Loading层 */
    export const Loading = 600;
    /**界面加载动画层，暂时放到最高层，加载动画时，界面未打开完成时，不让玩家点击其它地方 */
    export const UILoading = 700;
}

/**网络优先级,值越大，优化级越高 */
export enum NetPriority{
    Game,
    Chat,
    Lobby,
}